package com.milk.milkmedia.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.milk.milkmedia.R;
import com.umeng.community.example.CommunityActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 微安 on 2015/7/22.
 */
public class HomePageFragment extends Fragment implements View.OnClickListener{

    private Activity mAcitivity;
    private View mRootView;
    private ListView mListView;
    private Context mContext;
    private ArrayList<Map<String,Object>> mListData = new ArrayList<Map<String,Object>>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         mRootView = inflater.inflate(R.layout.fragment_start,container,false);
        mContext = getActivity();
        initView();
        return mRootView;
    }
    private void initView() {
        mListView = (ListView) mRootView.findViewById(R.id.mplay_home_list);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0){
                    Intent intent = new Intent(mAcitivity,MusicListAcitvity.class);
                    startActivity(intent);
                }else if(position == 1)
                {
                    Intent intent = new Intent(mAcitivity,CommunityActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(mContext,"暂未开发此功能敬请期待",Toast.LENGTH_SHORT).show();
                }
            }
        });
        getData();
    }

    private void getData() {
        String[] itemTexts = getResources().getStringArray(R.array.home_item);
        int[] itemImages = new int[]{R.drawable.ic_home_local,R.drawable.
                ic_home_collect,R.drawable.ic_home_study};
        for (int i = 0;i<itemTexts.length;i++)
        {
            HashMap<String,Object> map = new HashMap<String,Object>();
            map.put("text",itemTexts[i]);
            map.put("img",itemImages[i]);
            mListData.add(map);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(mContext,mListData,
                R.layout.item_home,new String[] { "text", "img" }, new int[]{
                R.id.mplay_home_tv, R.id.mplay_home_img });
        mListView.setAdapter(simpleAdapter);
    }


    @Override
    public void onAttach(Activity activity) {
        Log.d(HomePageActivity.Tag, "绑定事件");
        super.onAttach(activity);
        ((HomePageActivity) activity).onSectionAttached(getArguments().getInt(
                HomePageActivity.ARG_SECTION_NUMBER));
        this.mAcitivity = activity;
    }

    public static HomePageFragment newInstance(int sectionNum){
        HomePageFragment homePageFragment = new HomePageFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(HomePageActivity.ARG_SECTION_NUMBER, sectionNum);
        homePageFragment.setArguments(bundle);
        return homePageFragment;
    }


    public void  MusicListFragment(Activity activity) {
        Log.d(HomePageActivity.Tag, "创建绑定服务");
        this.mAcitivity = activity;
    }
    private void updataUI(){

    }

    @Override
    public void onClick(View v) {
    }

    
}
